﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;


namespace PhysicsSimulator
{
    class CVectorForce : CGameObject
    {
        private float _width = 0.0f;
        public float Width
        {
            get
            {
                return _width;
            }
            set
            {
                _width = value;
            }
        }

        private float _lenght = 0.0f;
        public float Lenght
        {
            get
            {
                return _lenght;
            }
            protected set
            {
                _lenght = value;
            }
        }

        private Color _color = Color.White;
        public Color Color
        {
            get
            {
                return _color;
            }
            set
            {
                _color = value;
            }
        }

        private Vector2 _position = Vector2.Zero;
        public Vector2 Position
        {
            get
            {
                return _position;
            }
            set
            {
                _position = value;
                _position.Y = 800 - _position.Y;
                UpdateForceData();
            }
        }

        private Vector2 _vectorForce = Vector2.Zero;
        public Vector2 VectorForce
        {
            get
            {
                return _vectorForce;
            }
            set
            {
                _vectorForce = value;
                _vectorForce.Y *= -1;
                UpdateForceData();
            }
        }

        private Matrix _worldMatrix;
        public Matrix WorldMatrix
        {
            get
            {
                return _worldMatrix;
            }
            set
            {
                _worldMatrix = value;
            }
        }

        private Matrix _viewMatrix;
        public Matrix ViewMatrix
        {
            get
            {
                return _viewMatrix;
            }
            set
            {
                _viewMatrix = value;
            }
        }

        private Matrix _projectionMatrix;
        public Matrix ProjectionMatrix
        {
            get
            {
                return _projectionMatrix;
            }
            set
            {
                _projectionMatrix = value;
            }
        }

        private BasicEffect _basicEffect;
        private GraphicsDevice _graphics;

        private VertexPositionColor[] _vertices = new VertexPositionColor[7];

        public CVectorForce(SpriteBatch spriteBatch) :
            base(spriteBatch)
        {
            _graphics = spriteBatch.GraphicsDevice;
            _basicEffect = new BasicEffect(_graphics);
            _basicEffect.TextureEnabled = false;
        }

        Vector3 cameraPosition;

        public Vector3 CameraPosition
        {
            get { return cameraPosition; }
            set { cameraPosition = value; }
        }
        Vector3 cameraTarget;

        public Vector3 CameraTarget
        {
            get { return cameraTarget; }
            set { cameraTarget = value; }
        }
        public override void Init(ContentManager contentManager)
        {

            cameraPosition = new Vector3(0,0,1);
            cameraTarget = Vector3.Zero;
            Vector3 cameraUp = Vector3.Up;            

            _worldMatrix = Matrix.Identity;

            _viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraTarget, cameraUp);

            _projectionMatrix = Matrix.CreateOrthographicOffCenter(
                0,
                _graphics.Viewport.Width,
                0,
                _graphics.Viewport.Height,
                0,
                1);
           

            base.Init(contentManager);
        }

        public override void Update(GameTimerEventArgs timer)
        {
            base.Update(timer);
        }

        public override void Draw(GameTimerEventArgs timer)
        {
            _basicEffect.World = _worldMatrix;
            _basicEffect.View = _viewMatrix;
            _basicEffect.Projection = _projectionMatrix;
            _basicEffect.VertexColorEnabled = true;

            foreach (EffectPass pass in _basicEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                _graphics.DrawUserIndexedPrimitives<VertexPositionColor>(
                    PrimitiveType.TriangleStrip,
                    _vertices,
                    0,
                    4,
                    new short[] { 0, 1, 2, 3 },
                    0,
                    2);

                _graphics.DrawUserIndexedPrimitives<VertexPositionColor>(
                    PrimitiveType.TriangleStrip,
                    new VertexPositionColor[] { _vertices[4], _vertices[5], _vertices[6] },
                    0,
                    3,
                    new short[] { 0, 1, 2 },
                    0,
                    1);

            }

        
            base.Draw(timer);
        }

        public virtual void Create(Vector2 position, Vector2 vectorForce, float width,Color color)
        {
            Position = position;
            _vectorForce = vectorForce;
            _width = width;
            _color = color;
            UpdateForceData();
        }

        public virtual void UpdateForceData()
        {
               
            Vector2 Po = _position;
            Vector2 Vo = _vectorForce + _position;

            Vector2 PoVo = Vector2.Subtract(Vo, Po);
            Vector2 uPoVo = Vector2.Normalize(PoVo);
            Vector2 nPoVo = new Vector2(-uPoVo.Y, uPoVo.X);

            _vertices[0].Position = new Vector3(Vector2.Add(nPoVo * Width / 2.0f,Po), 0);
            _vertices[0].Color = _color;

            _vertices[1].Position = new Vector3(Vector2.Add(nPoVo * Width / 2.0f,Vo),0);
            _vertices[1].Color = _color;

            _vertices[2].Position = new Vector3(Vector2.Add(-nPoVo * Width / 2.0f, Po), 0);
            _vertices[2].Color = _color;

            _vertices[3].Position = new Vector3(Vector2.Add(-nPoVo * Width / 2.0f, Vo), 0);
            _vertices[3].Color = _color;

            _vertices[4].Position = new Vector3(Vector2.Add(nPoVo * Width*2,Vo),0);
            _vertices[4].Color = _color;

            _vertices[5].Position = new Vector3(Vo.X + uPoVo.X * 5*_width,Vo.Y+ uPoVo.Y * 5 * _width,0);
            _vertices[5].Color = _color;

            _vertices[6].Position = new Vector3(Vector2.Add(-nPoVo * Width*2, Vo), 0);
            _vertices[6].Color = _color;
        }
    }
}
